package ysq.xyg.shop.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.base.util.EasyPoiUtil;
import ysq.xyg.shop.model.po.ShopBusiness;
import ysq.xyg.shop.model.po.ShopWithdraw;
import ysq.xyg.shop.model.vo.ShopBusinessExportVo;
import ysq.xyg.shop.model.vo.ShopBusinessVo;
import ysq.xyg.shop.model.vo.ShopWithdrawExportVo;
import ysq.xyg.shop.model.vo.ShopWithdrawVo;
import ysq.xyg.shop.service.ShopBusinessService;
import ysq.xyg.web.model.vo.WebMemberExportVo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 商城模块-交易表controller层
 *
 * @author congcong tao
 * @date 2019-4-28 11:19:14
 */
@RestController
public class ShopBusinessController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(ShopBusinessController.class);
    @Resource
    private ShopBusinessService shopBusinessService;

    private ShopBusiness shopBusiness;

    /**
     * 分页查询
     *
     * @param shopBusinessVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author congcong tao
     * @date 2019-4-28 11:19:14
     */
    @BaseLog(module = "商城模块",className = "交易表",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/shopBusiness/page",method = RequestMethod.POST)
    public ReturnEntity page(ShopBusinessVo shopBusinessVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = shopBusinessService.page(shopBusinessVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-4-28 11:19:14
     */
    @RequestMapping(value = "/shopBusiness/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(String id){
        ModelAndView view = new ModelAndView("/shop/shopBusinessUpdateOrSave");
        if(StrUtil.isNotEmpty(id)){
            shopBusiness = shopBusinessService.selectByPrimaryKey(id);
            view.addObject("shopBusiness", shopBusiness);
        }
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-4-28 11:19:14
     */
    @BaseLog(module = "商城模块",className = "交易表",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/shopBusiness/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return shopBusinessService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param shopBusinessVo Vo对象
     * @author congcong tao
     * @date 2019-4-28 11:19:14
     */
    @BaseLog(module = "商城模块",className = "交易表",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/shopBusiness/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,ShopBusinessVo shopBusinessVo){
        return shopBusinessService.addOrUpdate(shopBusinessVo,acceptCurrentUser(request));
    }

    /**
     * 导出
     * @param response
     * @param shopBusinessVo
     */
    @BaseLog(module = "商城模块",className = "提现表",methods = "导出",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping("/shopBusiness/export")
    public void export(HttpServletResponse response, ShopBusinessVo shopBusinessVo){
        if(StringUtil.isNotEmpty(shopBusinessVo.getMemberNickName())){
            shopBusinessVo.setMemberNickName(BaseUtil.encodeBase64String(shopBusinessVo.getMemberNickName()));
        }
        List<ShopBusiness> shopBusinessList = shopBusinessService.selectEntityList(shopBusinessVo);
        List<ShopBusinessExportVo> shopBusinessExportVoList = new ArrayList<ShopBusinessExportVo>();
        if (shopBusinessList != null && shopBusinessList.size() > 0) {
            for (ShopBusiness shopBusiness : shopBusinessList) {
                ShopBusinessExportVo shopBusinessExportVo = new ShopBusinessExportVo();
                String nickName = BaseUtil.decodeBase64(shopBusiness.getMemberNickName());
                BeanUtil.copyProperties(shopBusiness, shopBusinessExportVo);
                shopBusinessExportVo.setMemberNickName(nickName);
                // bean复制
                shopBusinessExportVoList.add(shopBusinessExportVo);
            }
        }
        String nowDate = DateUtil.formatDate(DateUtil.date());
        // 导出操作
        EasyPoiUtil.exportExcel(shopBusinessExportVoList, nowDate + "交易展示", "交易", WebMemberExportVo.class, nowDate + "交易展示.xls", response);
    }
}